package com.indusfo.spc.reportForms.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.indusfo.spc.config.PieceFlowConfig;
import com.indusfo.spc.reportForms.mapper.QualityAbnormalMapper;
import com.indusfo.spc.reportForms.pojo.QualityAbnoramlAll;
import com.indusfo.spc.reportForms.pojo.QualityAbnormal;
import com.indusfo.spc.reportForms.pojo.QualityAbnormalDto;
import com.indusfo.spc.reportForms.pojo.QualityAbnormaliInfoHistogram;
import com.indusfo.spc.reportForms.service.QualityAbnormalService;
import com.indusfo.spc.untlis.DateUtils;
import com.indusfo.spc.untlis.FileUtil;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * @ClassName QualityAbnormalServiceImpl
 * @Description
 * @Author louk
 * @Date 2020/2/7 13:57
 */
@Service
public class QualityAbnormalServiceImpl implements QualityAbnormalService {
    @Resource
    QualityAbnormalMapper qualityAbnormalMapper;
    @Override
    public JSONObject queryQualityAbnormaliInfo( QualityAbnormal qualityAbnormal) {
        QualityAbnoramlAll qualityAbnoramlAll=new QualityAbnoramlAll();

        String checkParam = checkParam(qualityAbnormal);
        if(!StringUtils.isEmpty(checkParam)){
            return JSONObject.build(JSONObject.ERROR,checkParam);
        }
        List<QualityAbnormalDto> qualityAbnormalDtoList = qualityAbnormalMapper.queryQualityAbnormaliInfo(qualityAbnormal);
        if(!qualityAbnormalDtoList.isEmpty()){
            qualityAbnoramlAll.setQualityAbnormalDtoList(qualityAbnormalDtoList);
            int countQualityAbnormaliInfo = qualityAbnormalMapper.countQualityAbnormaliInfo(qualityAbnormal);
            qualityAbnoramlAll.setCountQualityAbnormaliInfo(countQualityAbnormaliInfo);
        }
        List<QualityAbnormaliInfoHistogram> qualityAbnormaliInfoHistogramList = qualityAbnormalMapper.queryQualityAbnormaliInfoHistogram(qualityAbnormal);
        if(!qualityAbnormaliInfoHistogramList.isEmpty()){
            qualityAbnoramlAll.setQualityAbnormaliInfoHistogramList(qualityAbnormaliInfoHistogramList);
        }
        if(qualityAbnoramlAll!=null){
            return JSONObject.build(JSONObject.SUCCESS,"查询成功",qualityAbnoramlAll);
        }
        return JSONObject.build(JSONObject.SUCCESS,"暂无相关数据",qualityAbnoramlAll);
    }

    @Override
    public void downloadQualityAbnormaliInfo(QualityAbnormal qualityAbnormal, HttpServletResponse res, HttpServletRequest request) {
        String checkParam = checkParam(qualityAbnormal);
        if(StringUtils.isEmpty(checkParam)){
            List<QualityAbnormalDto> qualityAbnormalDtoList = qualityAbnormalMapper.queryQualityAbnormaliInfo(qualityAbnormal);
            String target = PieceFlowConfig.getProfile();
            String name="Excel导出"+DateUtils.dateTimeNow()+".xlsx";
            // 写法2
            String fileName = target+name;
            try {
                FileUtils.createTmpFile(fileName);
                //随意创建一个Excel
//                FileUtil.createExcel(fileName);
                // 这里 需要指定写用哪个class去写
                ExcelWriter excelWriter = EasyExcel.write(fileName, QualityAbnormalDto.class).build();
                WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
                excelWriter.write(qualityAbnormalDtoList, writeSheet);
                /// 千万别忘记finish 会帮忙关闭流
                excelWriter.finish();
                FileUtil.downloadExcel(name,res,request);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    public  String checkParam(QualityAbnormal qualityAbnormal){
        Integer pagesize = qualityAbnormal.getPagesize();
        Integer pageindex = qualityAbnormal.getPageindex();
        if(pagesize!=null && !"".equals(pagesize) && pageindex!=null && !"".equals(pageindex)){
            qualityAbnormal.setIncept(pagesize * (pageindex - 1));
        }
        String startDate = qualityAbnormal.getStartDate();
        String endDate = qualityAbnormal.getEndDate();
        if((StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) || (!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate))){
            return "开始时间结束时间必须同时存在";
        }
        if(!StringUtils.isEmpty(startDate)){
            boolean validDate = DateUtils.isValidDate(startDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            if(!validDate){
                return "开始时间格式不正确";
            }
        }else{
            qualityAbnormal.setStartDate(DateUtils.dateSplicer("08:00:00"));
        }
        if(!StringUtils.isEmpty(endDate)){
            boolean validDate = DateUtils.isValidDate(endDate, DateUtils.YYYY_MM_DD_HH_MM_SS);
            if(!validDate){
                return "结束时间格式不正确";
            }
        }else{
            qualityAbnormal.setEndDate(DateUtils.dateSplicer(DateUtils.dateByDay(1),"08:00:00"));
        }
        return null;
    }
}
